package view;

import model.StudentProfile;
import service.StudentProfileService;
import javax.swing.*;
import com.toedter.calendar.JDateChooser;
import java.awt.*;
import java.util.Date;
import javax.swing.border.EmptyBorder;
import java.text.SimpleDateFormat;

public class StudentProfilePanel extends JPanel {
    private int userId;
    private StudentProfileService studentProfileService;
    private JTextField studentNoField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField classField;
    private JTextField majorField;
    private JTextField departmentField;
    private JTextField enrollmentYearField;
    private JTextField phoneField;
    private JTextField emailField;
    private JDateChooser birthDateChooser;
    private JTextArea addressArea;
    
    public StudentProfilePanel(int userId) {
        this.userId = userId;
        this.studentProfileService = new StudentProfileService();
        initComponents();
        loadStudentProfile();
    }
    
    private void initComponents() {
        setLayout(new BorderLayout(10, 10));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建表单面板
        JPanel formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        
        // 创建输入组件
        studentNoField = createReadOnlyField();
        nameField = createReadOnlyField();
        genderField = createReadOnlyField();
        classField = createReadOnlyField();
        majorField = createReadOnlyField();
        departmentField = createReadOnlyField();
        enrollmentYearField = createReadOnlyField();
        phoneField = new JTextField(20);
        emailField = new JTextField(20);
        birthDateChooser = new JDateChooser();
        addressArea = new JTextArea(3, 20);
        
        // 添加到表单
        int row = 0;
        addFormField(formPanel, "学号：", studentNoField, gbc, row++);
        addFormField(formPanel, "姓名：", nameField, gbc, row++);
        addFormField(formPanel, "性别：", genderField, gbc, row++);
        addFormField(formPanel, "系部：", departmentField, gbc, row++);
        addFormField(formPanel, "专业：", majorField, gbc, row++);
        addFormField(formPanel, "班级：", classField, gbc, row++);
        addFormField(formPanel, "入学年份：", enrollmentYearField, gbc, row++);
        addFormField(formPanel, "联系电话：", phoneField, gbc, row++);
        addFormField(formPanel, "电子邮箱：", emailField, gbc, row++);
        addFormField(formPanel, "出生日期：", birthDateChooser, gbc, row++);
        
        // 家庭住址使用文本区域
        gbc.gridx = 0;
        gbc.gridy = row;
        formPanel.add(new JLabel("家庭住址："), gbc);
        
        gbc.gridx = 1;
        addressArea.setLineWrap(true);
        addressArea.setWrapStyleWord(true);
        JScrollPane addressScroll = new JScrollPane(addressArea);
        addressScroll.setPreferredSize(new Dimension(200, 60));
        formPanel.add(addressScroll, gbc);
        
        add(formPanel, BorderLayout.CENTER);
        
        // 添加保存按钮
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(e -> saveProfile());
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(saveButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private JTextField createReadOnlyField() {
        JTextField field = new JTextField(20);
        field.setEditable(false);
        field.setBackground(new Color(240, 240, 240));
        return field;
    }
    
    private void loadStudentProfile() {
        StudentProfile profile = studentProfileService.getStudentProfile(userId);
        if (profile != null) {
            studentNoField.setText(profile.getStudentNo());
            nameField.setText(profile.getName());
            genderField.setText(profile.getGender());
            classField.setText(profile.getClassName());
            majorField.setText(profile.getMajorName());
            departmentField.setText(profile.getDepartmentName());
            enrollmentYearField.setText(String.valueOf(profile.getEnrollmentYear()));
            phoneField.setText(profile.getPhone());
            emailField.setText(profile.getEmail());
            birthDateChooser.setDate(profile.getBirthDate());
            addressArea.setText(profile.getHomeAddress());
        }
    }
    
    private void saveProfile() {
        StudentProfile profile = studentProfileService.getStudentProfile(userId);
        if (profile != null) {
            // 只更新可编辑的字段
            profile.setPhone(phoneField.getText().trim());
            profile.setEmail(emailField.getText().trim());
            profile.setBirthDate(birthDateChooser.getDate());
            profile.setHomeAddress(addressArea.getText().trim());
            
            if (studentProfileService.updateStudentProfile(profile)) {
                JOptionPane.showMessageDialog(this, "保存成功！");
            } else {
                JOptionPane.showMessageDialog(this, "保存失败！", 
                    "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    private void addFormField(JPanel panel, String label, JComponent field, 
            GridBagConstraints gbc, int row) {
        gbc.gridx = 0;
        gbc.gridy = row;
        gbc.anchor = GridBagConstraints.EAST;
        JLabel jLabel = new JLabel(label);
        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        panel.add(jLabel, gbc);
        
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(field, gbc);
    }
} 